//
//  XMBaseViewController.m
//  XMNewBeeBaseController
//
//  Created by new-1020 on 2017/3/20.
//  Copyright © 2017年 new-1020. All rights reserved.
//
#define FooterHeight 40.0
#define HeaderHeight 50.0
#import "XMBaseViewController.h"

@interface XMBaseViewController ()<UITableViewDelegate>
@property (nonatomic,strong) UIView * nodataView;
@property (nonatomic,strong) UILabel *BottomLabel;
@property (nonatomic,strong) UILabel *headLabel;
@end

@implementation XMBaseViewController
-(UITableView *)XMTableview {
    if (_XMTableview == nil) {
        CGRect tableFrame;
        if (self.navigationController) {
            tableFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) ;
        }else {
            tableFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) ;
        }
        _XMTableview = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
        _XMTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _XMTableview.delegate = self;
        _XMTableview.dataSource = self;
        _XMTableview.tableFooterView = [[UIView alloc]init];
    }
    return _XMTableview;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [[UITableViewCell alloc]init];
    return cell;
}

-(UIView *)nodataView {
    if (_nodataView == nil) {
        _nodataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        _nodataView.userInteractionEnabled = NO;
        UILabel * label  = [[UILabel alloc]init];
        label.text = @"暂无数据";
        [self.nodataView addSubview:label];
        label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        label.font = [UIFont systemFontOfSize:15];
        [label sizeToFit];
        label.center = _nodataView.center;
    }
    return _nodataView;
}

-(NSMutableArray *)baseMutableDataArray {
    if (_baseMutableDataArray == nil) {
        _baseMutableDataArray = [[NSMutableArray alloc]init];
    }
    return  _baseMutableDataArray;
}

-(void)setShowNoDataView:(BOOL)showNoDataView {
    _showNoDataView = showNoDataView;
    [self showCutomNOdataView:showNoDataView];
}

-(void)showCutomNOdataView:(BOOL)show {
    if (show) {
        if (self.CustomNodataView != nil) {
            [self.view addSubview:self.CustomNodataView];
            self.CustomNodataView.center = self.view.center;
        }else {
            [self.view addSubview:self.nodataView];
        }
    }else {
        [self.CustomNodataView removeFromSuperview];
        [self.nodataView removeFromSuperview];
    }
}

//隐藏line
- (void)XMNavgationBarLineHidden:(BOOL)hidden {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)])
    {
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list)
        {
            if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0)
            {//10.0的系统字段不一样
                UIView *view =   (UIView*)obj;
                for (id obj2 in view.subviews) {

                    if ([obj2 isKindOfClass:[UIImageView class]])
                    {
                        UIImageView *image =  (UIImageView*)obj2;
                        if (image.height < 2) {
                            image.hidden = hidden;
                        }
                    }
                }
            }else{
                if ([obj isKindOfClass:[UIImageView class]]) {
                    UIImageView *imageView = (UIImageView *)obj;
                    NSArray *list2 = imageView.subviews;
                    for (id obj2 in list2) {
                        if ([obj2 isKindOfClass:[UIImageView class]]) {
                            UIImageView *imageView2 = (UIImageView *)obj2;
                            if (imageView2.height < 2) {
                                imageView2.hidden = hidden;
                            }
                        }
                    }
                }
            }
        }
    }
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.hideNavLine) {
        [self XMNavgationBarLineHidden:YES];
    }
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.hideNavLine) {
        [self XMNavgationBarLineHidden:NO];
    }
}


-(void)dealloc {
    if (self.childClassName) {
        NSLog(@"[%@ dealloc]",self.childClassName);
    }else {
        NSLog(@"[baseVC dealloc]");
    }
}

-(void)setLeftButton:(UIButton *)leftButton {
    _leftButton = leftButton;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
}

-(void)setRightButton:(UIButton *)rightButton {
    _rightButton = rightButton;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
}


@end
